//方块的类
(function(window) {
    class eachBlock extends Render { //单个小方块    渲染类的子类
        constructor(x, y, bgColor) { //渲染一个方块需要知道左侧偏移、顶部偏移、背景颜色
            super(x, y, bgColor);
            this.x = x; //左侧相对偏移
            this.y = y; //顶部相对偏移
            this.bgColor = bgColor;
        }
    }
    class Blockbody { //某种状态的俄罗斯方块，即一个可以被确定的俄罗斯方块
        constructor(x, y, bgC) {
            this.block = []; //方块的每一块
            for (let i = 0; i < 4; i++) { //4个小方块的相对偏移
                this.block[i] = new eachBlock(x[i], y[i], bgC);
            }
            this.rendered = []; //记录这个俄罗斯方块的dom对象
        }
        render = function(father, offsetX, offsetY) { //渲染这个俄罗斯方块的方法
            for (let i = 0; i < 4; i++) {
                this.rendered.push(this.block[i].render(father, offsetX, offsetY));
            }
            return this.rendered; //返回这个俄罗斯方块的dom对象
        }
        remove = function() {
            for (let i = 0; i < this.rendered.length; i++) {
                this.block[i].remove();
            }
            this.rendered.length = 0;
        }
    }
    class Block { //方块的类
        constructor(x, y, ntype, current, bgC) { //一类俄罗斯方块，包含几种状态的偏移量，包含几种俄罗斯方块，当前指向的是哪一个
            this.x = x;
            this.y = y;
            this.type = ntype;
            this.state = current;
            this.rendered = [];
            this.blockBody = [];
            for (let i = 0; i < ntype; i++) { //有ntype种变换形态
                this.blockBody[i] = new Blockbody(x[i], y[i], bgC);
            }
        }
        render = function(father, offsetX, offsetY) {
            return this.rendered = this.blockBody[this.state].render(father, offsetX, offsetY);
        }
        remove = function() {
            if (this.rendered.length) {
                this.blockBody[this.state].remove();
            }
        }
        change = function(next) { //变换当前指向的俄罗斯方块的方法
            this.state = next;
        }
    }
    window.Block = Block;
}(window))